package com.holi.hiteacher.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.RadioGroup;

import com.holi.hiteacher.R;
import com.holi.hiteacher.fragment.MiiFragment;
import com.holi.hiteacher.fragment.SchoolInfoFragment;
import com.holi.hiteacher.fragment.StudyCircleFragment;
import com.holi.hiteacher.other.ViewPaperAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_view_paper)
    ViewPager mViewPager;
    @BindView(R.id.id_main_tab)
    RadioGroup mRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initToolbar();

        SchoolInfoFragment ftSchool = new SchoolInfoFragment();
        StudyCircleFragment ftStudy = new StudyCircleFragment();
        MiiFragment ftMii = new MiiFragment();

        List<Fragment> fgList = new ArrayList<>();
        fgList.add(ftSchool);
        fgList.add(ftStudy);
        fgList.add(ftMii);

        mViewPager.setAdapter(new ViewPaperAdapter(getSupportFragmentManager(), fgList));
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setCurrentItem(1);
        setTitle(R.string.study_circle);

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.id_main_tab_school_info:
                        mViewPager.setCurrentItem(0, true);
                        setTitle(R.string.school_info2);
                        break;
                    case R.id.id_main_tab_study_circle:
                        mViewPager.setCurrentItem(1, true);
                        setTitle(R.string.study_circle);
                        break;
                    case R.id.id_main_tab_mii:
                        mViewPager.setCurrentItem(2, true);
                        setTitle(R.string.mii);
                        break;
                }
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        mRadioGroup.check(R.id.id_main_tab_school_info);
                        break;
                    case 1:
                        mRadioGroup.check(R.id.id_main_tab_study_circle);
                        break;
                    case 2:
                        mRadioGroup.check(R.id.id_main_tab_mii);
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
//        mToolbar.setTitle(getString(R.string.mii));
//        mToolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_white);
//        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//            }
//        });
    }

}
